package com.haois.mp.database.module.sdk.api;

import com.haois.mp.database.commom.enums.ApiResultEnum;
import com.haois.mp.database.commom.constant.Reply;

import com.haois.mp.database.util.HttpClientUtil;
import com.haois.mp.database.commom.constant.ApiAppKey;
import com.haois.mp.database.module.common.daomain.Driver;
import com.haois.mp.database.module.common.daomain.JsonFor;
import com.haois.mp.database.module.common.daomain.Peccancy;
import com.haois.mp.database.module.common.daomain.Vehicle;
import com.haois.mp.wechat.common.JobLogCollect;
import com.xxl.job.core.log.XxlJobLogger;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * description
 *
 * @author RuiHaoZ on GuangZhou
 * @date 2018/9/4 21:40
 */
public class DriverInfoServer {

    private static Logger logger = LoggerFactory.getLogger(DriverInfoServer.class);

    public static String getDriverString(Driver driver) {
        String host = "https://jisujszkf.market.alicloudapi.com";
        String path = "/driverlicense/query";
        String method = "GET";
        String appcode = ApiAppKey.Driver_App_Code;
        Map<String, String> headers = new HashMap<>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> params = new HashMap<>();
        params.put("licenseid", driver.getDossier());
        params.put("licensenumber", driver.getLicenseNo());
        try {
//            HttpResponse response = HttpUtils.doGet(host, path, method, headers, params);
//            String result1 = EntityUtils.toString(response.getEntity());
            //获取response的body
            String result = HttpClientUtil.sendBaseGet(host + path, params, headers);
            XxlJobLogger.log("杭州网尚科技有限公司的驾驶证扣分查询接口返回结果为：" + result);
            logger.info("杭州网尚科技有限公司的驾驶证扣分查询接口返回结果为：" + result);
//            String result = "{\"status\":\"0\",\"msg\":\"ok\",\"result\":{\"licensenumber\":\"410326199303227534\",\"licenseid\":\"410331376583\",\"realname\":\"张睿浩\",\"type\":\"C1\",\"score\":\"9\",\"startdate\":\"2014-08-28 00:00:00\",\"enddate\":\"2020-08-28 00:00:00\"}}";
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Reply getDriverInfo(Driver driver) {
        Driver d1 = JsonFor.driverTransfer(getDriverString(driver));
        if (d1.getCode().equals("0")) {
            return new Reply<>(ApiResultEnum.API_00, d1);
        } else {
            return new Reply<>(ApiResultEnum.API_10.getCode(), d1.getCode(), d1);
        }

    }

    public static Reply getVehicleInfo(Vehicle vehicle, boolean isDirect) {
        String reCode = ApiResultEnum.API_00.getCode();
        String remMsg = ApiResultEnum.API_00.getMsg();
        JSONObject jsonObject = null;
        if (isDirect) {
            jsonObject = JhWzService.getVehicleWzInfo_Swei(vehicle);
        } else {
            jsonObject = JhWzService.getVehicleWzInfo(vehicle);
        }
        String code = jsonObject.getString("error_code");
        String reason = jsonObject.getString("reason");
        if (jsonObject != null) {
            if (code.equals("0")) {
                vehicle.setCode(code);
                List<Peccancy> peccancies = JsonFor.pecTransfer(jsonObject);
                vehicle.setPeccancies(peccancies);
            } else {
                reCode = code + ":" + reason;
                reCode = ApiResultEnum.API_10.getCode();
            }
        } else {
            reCode = "nothing";
            reCode = ApiResultEnum.API_10.getCode();
        }
        return new Reply(reCode, remMsg, vehicle);
    }

}
